package com.wanghe.leetcode.mianshijindian;

import java.util.ArrayList;
import java.util.List;

/**
 * 面试题01.06 字符串压缩
 * https://leetcode-cn.com/problems/compress-string-lcci/
 */
public class Problem0106 {

    class CounterData {
        private Character c;
        private Integer couter;

        public Character getC() {
            return c;
        }

        public void setC(Character c) {
            this.c = c;
        }

        public Integer getCouter() {
            return couter;
        }

        public void setCouter(Integer couter) {
            this.couter = couter;
        }
    }

    public String compressString(String S) {
        List<CounterData> counterList = new ArrayList<>();
        for (int i = 0; i < S.length(); i++) {
            Character c = S.charAt(i);
            Integer curCounter = 0;
            while (i < S.length()) {
                Character next = S.charAt(i);
                if (next.equals(c)) {
                    curCounter++;
                    i++;
                } else {
                    break;
                }
            }
            CounterData cd = new CounterData();
            cd.setC(c);
            cd.setCouter(curCounter);
            counterList.add(cd);
            if (i < S.length()) {
                i--;
            }
        }

        StringBuilder sb = new StringBuilder();
        for (CounterData data : counterList) {
            sb.append(data.getC()).append(data.getCouter());
        }
        String compressedString =  sb.toString();
        if (compressedString.length() >= S.length()) {
            return S;
        }
        return compressedString;
    }

    public static void main(String[] args) {
        Problem0106 problem0106 = new Problem0106();
//        System.out.println(problem0106.compressString("aabcccccaaa"));
        System.out.println(problem0106.compressString("abbccd"));
    }

}
